package mashuaiproject.mashuaiproject.controller;
import mashuaiproject.mashuaiproject.model.clickfabulou;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.FabulousSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class FabulousContrller {
    @Autowired
    private FabulousSerivce fabulousSerivce;
    @ResponseBody
    @GetMapping("/Fabulous")
    public Map<String ,Object> fabulous(@RequestParam("fatherId")Integer  fatherId,@RequestParam("type") String  type
    , HttpServletRequest request,@RequestParam("type_son")Integer type_son,@RequestParam("quesionid")Integer quesionid){

        Integer countfabulou=0;//默认的携带总数
        String stause=null;//判断是哪个按钮
        System.out.println(fatherId+"--"+type);
        Map<String ,Object> map=new HashMap<>();
        //获取当前用户
        Users users= (Users) request.getSession().getAttribute("user");
        if(users==null){
            map.put("code",2004);//未登录
            return map;
        }



        map.put("code",2005);//未登录
        //根据评论的id。查找该操作是否已经点赞
        //封装对象
        clickfabulou fabulous=new clickfabulou();
        fabulous.setComment_id(fatherId);
        fabulous.setUser_id(users.getId());
        fabulous.setGmt_create(System.currentTimeMillis());
        fabulous.setGmt_modified(fabulous.getGmt_create());
        fabulous.setQuesionid(quesionid);

      if(type.equals("up")){
            //点赞up操作,,,在判断是点赞+1，还是-1
            //如果数据库为空说明第一次对这个评论操作
           Boolean isFabulous=fabulousSerivce.isClickFabulous(fatherId);
           if(isFabulous){
               if (type_son==1) {//点赞
                   //第一次操作,执行插入操作
                   fabulous.setFabulous_up("true");//这是点赞+1操作
                   fabulous.setFabulous_down("false");//没有点击点赞下将
                   fabulousSerivce.insertfabulous(fabulous);
                   //第一次点赞
                   //查找更新comment表的点赞数
                   fabulousSerivce.updateUpCommentFabulous(fatherId,"add","up");

               }else {
                   System.out.println("第一次进来不可能直接点击取消的");
               }
           }else{
               //不是第一次操作,执行更新操作.将fabulous_up=true
               if(type_son==1){
                   fabulousSerivce.updatefabulous(fatherId,"up");//点击赞+1
                   fabulousSerivce.updateUpCommentFabulous(fatherId,"add","up");
               }else {
                   fabulousSerivce.updatefabulous(fatherId,"up_no");//取消点赞
                   fabulousSerivce.updateUpCommentFabulous(fatherId,"reduces","up");
               }

           }

           //携带当前的用户点赞总数
          int fabulous_up=fabulousSerivce.getabulous_up(fatherId);
          countfabulou=fabulous_up;
          stause="up";

        }

        if(type.equals("down")){
            Boolean isFabulous=fabulousSerivce.isClickFabulous(fatherId);
            if(isFabulous){
                //第一次操作,执行插入操作
                if(type_son==1) {
                    fabulous.setFabulous_up("false");//这是点赞+1操作
                    fabulous.setFabulous_down("true");//没有点击点赞下将
                    fabulousSerivce.insertfabulous(fabulous);
                    //更新踩赞数
                    fabulousSerivce.updateUpCommentFabulous(fatherId,"add","down");
                }else {
                    System.out.println("第一次进来不可能直接点击取消的");
                }
            }else{
                //不是第一次操作,执行更新操作.将fabulous_up=true
                if(type_son==1){
                    fabulousSerivce.updatefabulous(fatherId,"down");//增加点赞下将一
                    fabulousSerivce.updateUpCommentFabulous(fatherId,"add","down");

                }else{
                    fabulousSerivce.updatefabulous(fatherId,"up_no");//取消点赞下将一
                    fabulousSerivce.updateUpCommentFabulous(fatherId,"reduces","down");
                }
            }
            //携带当前用户的总点赞
            Integer fabulous_down=fabulousSerivce.getabulous_down(fatherId);
            countfabulou=fabulous_down;
            stause="down";


        }
        //在判断是点赞还是取消点赞

        map.put("stause",stause);
        map.put("countfabulou",countfabulou);
        map.put("ket","测试");
        return  map;
    }
}
